package com.liu.community.server.impl;/*
 *@Author:Liu Xing-zhi
 *Description:
 *Date:Crated in 11:40 2021/10/8
 */

import com.liu.community.dto.PageDto;
import com.liu.community.dto.QuestionDto;
import com.liu.community.exception.CustomerException;
import com.liu.community.exception.ErrorCode;
import com.liu.community.mapper.QuestionMapper;
import com.liu.community.mapper.UserMapper;
import com.liu.community.pojor.Question;
import com.liu.community.pojor.User;
import com.liu.community.server.QuestionService;
import com.liu.community.utils.StringUtilByLike;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public void createQuestion(Question question) {
        questionMapper.createQuestion(question);
    }

   /* @Override
    public List<QuestionDto> queryAllQuestion() {
        List<QuestionDto> questionDtoList = new ArrayList<>();
        //查询question
        List<Question> questionList = questionMapper.queryAllQuestion();
        //遍历该集合
       for (Question question : questionList) {
            //根据id查询用户信息。
            User user = userMapper.queryUserById(question.getCreator());
            //将question 对象转化为questionDto
            QuestionDto questionDto = new QuestionDto();
            //根据spring方法，将question属性复制到questionDto
            BeanUtils.copyProperties(question,questionDto);
            questionDto.setUser(user);
            //将questionDto加入到list中
            questionDtoList.add(questionDto);
        }
        return  questionDtoList;
    }
*/
    @Override
    public PageDto<QuestionDto> queryAllQuestions(String tag,String search,Integer currentPage, Integer pageSize) {
        String searchLike = StringUtilByLike.getSearch(search);
        //将tag分开
        String tagLike = StringUtilByLike.getSearch(tag);
        PageDto<QuestionDto> pageDto = new PageDto<>();
        List<QuestionDto> questionDtoList = new ArrayList<>();
        //求总记录数
        Integer dataTotalCount = questionMapper.queryDataTotalCount(tagLike,searchLike);
        //求begin边界
        Integer begin =  (currentPage - 1) * pageSize;
        List<Question> questionList = questionMapper.queryAllQuestion(tagLike,searchLike,begin, pageSize);
        //遍历集合
        for (Question question : questionList) {
            User user = userMapper.queryUserById(question.getCreator());
            //将question 对象转化为questionDto
            QuestionDto questionDto = new QuestionDto();
            BeanUtils.copyProperties(question,questionDto);
            questionDto.setUser(user);
            questionDtoList.add(questionDto);
        }
        pageDto.setData(questionDtoList);
        pageDto.setPageMessage(currentPage,pageSize,dataTotalCount,pageDto);
        return pageDto;
    }

    @Override
    public PageDto<QuestionDto> list(Integer userId, Integer currentPage, Integer pageSize) {
        PageDto<QuestionDto> pageDto = new PageDto<>();
        List<QuestionDto> questionDtoList = new ArrayList<>();
        //求总记录数
        Integer dataTotalCount = questionMapper.queryDataTotalCountByUserId(userId);
        //求begin边界
        Integer begin =  (currentPage - 1) * pageSize;
        List<Question> questionList = questionMapper.queryAllQuestionById(userId,begin, pageSize);
        //遍历集合
        for (Question question : questionList) {
            User user = userMapper.queryUserById(question.getCreator());
            //将question 对象转化为questionDto
            QuestionDto questionDto = new QuestionDto();
            BeanUtils.copyProperties(question,questionDto);
            questionDto.setUser(user);
            questionDtoList.add(questionDto);
        }
        pageDto.setData(questionDtoList);
        pageDto.setPageMessage(currentPage,pageSize,dataTotalCount,pageDto);
        return pageDto;
    }

    @Override
    public QuestionDto queryQuestionById(Integer id) {
        Question question = questionMapper.queryQuestionById(id);
        if (question == null){
            throw  new CustomerException(ErrorCode.QUESTION_NOT_FOUND);
        }
        QuestionDto questionDto = new QuestionDto();
        BeanUtils.copyProperties(question,questionDto);
        User user = userMapper.queryUserById(question.getCreator());
        questionDto.setUser(user);
        return questionDto;
    }

    @Override
    public void createOrUpdateQuestion(Question question) {
        if(question.getId() == null){
            //id为空,新增
            question.setGmtCreate(System.currentTimeMillis());
            question.setGmtModified(question.getGmtCreate());
            questionMapper.createQuestion(question);
        }else {
            //id不为空，修改
            question.setGmtModified(System.currentTimeMillis());
            int update = questionMapper.update(question);
            if (update != 1){
                throw  new CustomerException(ErrorCode.QUESTION_NOT_FOUND);
            }
        }
    }

    @Override
    public void increViewCount(Integer id) {
        Question question = questionMapper.queryQuestionById(id);
        Question updateQuestion = new Question();
        updateQuestion.setViewCount(1);
        updateQuestion.setId(id);
        questionMapper.updateViewCount(updateQuestion);
    }

    @Override
    public List<QuestionDto> queryRelated(QuestionDto queryDto) {
        if (StringUtils.isBlank(queryDto.getTag())){
            return  new ArrayList<>();
        }
        //将tag分开
        String[] tags = StringUtils.split(queryDto.getTag(), ",");
        String regexpTag = Arrays.stream(tags).collect(Collectors.joining("|"));
        Question question = new Question();
        question.setId(queryDto.getId());
        question.setTag(regexpTag);
        List<Question> questions = questionMapper.queryRelated(question);
        //将question转换为questionDto
        List<QuestionDto> questionDtos = questions.stream().map(q -> {
            QuestionDto questionDto = new QuestionDto();
            BeanUtils.copyProperties(q,questionDto);
            return questionDto;
        }).collect(Collectors.toList());
        return questionDtos;
    }
}
